package st.business.tepth.business.taskallocation.station;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.ModifyStationBean;
import st.business.tepth.business.event.ReloadLocationEvent;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.dialog.IDialogClickListener;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-07-17 17:46
 * 调整工位
 */

public class StationDelegate extends MvpFragment<StationPresenter> implements StationContract.View {
    public static StationDelegate newInstance(int repairId) {
        Bundle bundle = new Bundle();
        bundle.putInt("repairId", repairId);
        StationDelegate delegate = new StationDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.tv_bus_number)
    TextView mTvBusNumber;
    @BindView(R2.id.tv_work_type)
    TextView mTvWorkType;
    @BindView(R2.id.tv_current_station)
    TextView mTvCurrentStation;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R2.id.tv_tile)
    TextView mTvTitle;
    private int mRepairId;
    private ModifyStationBean.Station mCurrentSelectedStation;
    private int mOldSelectedPosition = -1;
    private BaseQuickAdapter<ModifyStationBean.Station, BaseViewHolder> mAdapter;
    private DialogLoader mDialogLoader;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mRepairId = bundle.getInt("repairId");
        }
    }

    @Override
    public StationPresenter getPresenter() {
        return new StationPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_modify_location;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mRecyclerView.setLayoutManager(new GridLayoutManager(_mActivity, 4));
        RecyclerView.ItemAnimator itemAnimator = mRecyclerView.getItemAnimator();
        if (itemAnimator != null) {
            itemAnimator.setChangeDuration(0);
        }
        mRecyclerView.setAdapter(mAdapter = new BaseQuickAdapter<ModifyStationBean.Station, BaseViewHolder>
                (R.layout.item_modify_station) {
            @Override
            protected void convert(final BaseViewHolder helper, final ModifyStationBean.Station item) {
                final CheckBox checkBox = helper.getView(R.id.check_person);
                checkBox.setChecked(item.isChecked());
                checkBox.setText(item.getWorkPlaceName());
                helper.setText(R.id.tv_state, "(" + item.getStatusDesc() + ")");
                helper.getView(R.id.layout_checkbox).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!item.isChecked()) {
                            mCurrentSelectedStation = item;
                            item.setChecked(true);
                            helper.setChecked(R.id.check_person, true);
                            if (mOldSelectedPosition != -1) {
                                ModifyStationBean.Station oldItem = getItem(mOldSelectedPosition);
                                if (oldItem != null) {
                                    oldItem.setChecked(false);
                                    notifyItemChanged(mOldSelectedPosition);
                                }
                            } else {
                                changeConfirmState(true);
                            }
                            mOldSelectedPosition = helper.getAdapterPosition();
                        }

                    }
                });
            }
        });
        mTvTitle.setText("调整工位");
        changeConfirmState(false);
        mDialogLoader = new DialogLoader(_mActivity);
        mPresenter.getWorkPlace(UserManager.getInstance().getToken(), mRepairId);
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }

    @Override
    public void showData(ModifyStationBean modifyStationBean) {
        if (modifyStationBean != null) {
            mTvBusNumber.setText(String.format("车辆编号：%s",
                    modifyStationBean.getVehicleNo()));
            mTvWorkType.setText(String.format("作业类型：%s",
                    modifyStationBean.getRepairType()));
            mTvCurrentStation.setText(String.format("当前工位：%s",
                    modifyStationBean.getCurrentWorkPlace()));
            mAdapter.setNewData(modifyStationBean.getList());
        }
    }

    @Override
    public void modifySucceed() {
        mDialogLoader.stopProgressWithSuccess("修改成功", "请确认", new IDialogClickListener() {
            @Override
            public void onClick() {
                EventBus.getDefault().post(new ReloadLocationEvent());
                mDialogLoader.clearDialog();
                pop();
            }
        });
    }

    @Override
    public void showError(String msg) {
        mDialogLoader.stopProgressWithFailed(msg, msg);
    }

    @OnClick(R2.id.tv_confirm)
    public void onModifyStation() {
        if (mCurrentSelectedStation != null) {
            if (mCurrentSelectedStation.getStatusCode() == 1) {
                mDialogLoader.showWarning("请确认", "所选工位已有车，将进行对换",
                        new IDialogClickListener() {
                            @Override
                            public void onClick() {
                                mDialogLoader.changeToProgressType("正在提交...");
                                mPresenter.modifyStation(UserManager.getInstance().getToken(),
                                        mRepairId, mCurrentSelectedStation.getWorkPlaceId());
                            }
                        });
            } else {
                mDialogLoader.showProgressDialog(false, "正在提交", "请稍候...");
                mPresenter.modifyStation(UserManager.getInstance().getToken(),
                        mRepairId, mCurrentSelectedStation.getWorkPlaceId());
            }
        }
    }

    private void changeConfirmState(boolean hasSelected) {
        mTvConfirm.setBackgroundResource(hasSelected ? R.drawable.selector_btn_operation :
                R.drawable.shape_btn_operation_t);
        mTvConfirm.setTextColor(hasSelected ? Color.parseColor("#333333") :
                Color.parseColor("#ffffff"));
        mTvConfirm.setEnabled(hasSelected);
    }
}